/*
 * Flux JavaScript Library
 * Copyright (c) 2010 Leonardo Dutra Constancio
 * MIT License (http://jsflux.googlecode.com/svn/trunk/mit-license.txt)
 */

flux.Object2D = function()
{
	if(this instanceof flux.Object2D)
	{
		flux.EventDispatcher.call(this);
		this.x = this.y = this.rotation = 0;
		this.hidden = false;
		this.name = '';
		this._children = [];
		this._parent = null;
		this.setIntensity(1);
	}
};
flux.Object2D.prototype = new flux.EventDispatcher;

// optimization
flux.Object2D.prototype.setXY = function(x, y)
{
	this.x = x;
	this.y = y;
};
flux.Object2D.prototype.getGlobalXY = function()
{
    var x = this.x;
    var y = this.y;
	var o = this._parent;
	while(o)
	{
		x += o.x;
		y += o.y;
		o = o._parent;
	}
	return new flux.V2(x, y);
};
flux.Object2D.prototype.setRotationAngle = function(angle)
{
	this.rotation = 6.283185307179586 * angle / 360; // 6.283185307179586 = Math.PI * 2;
};
flux.Object2D.prototype.getRotationAngle = function()
{
	return this.rotation * 360 / 6.283185307179586; // 6.283185307179586 = Math.PI * 2;
};
flux.Object2D.prototype.setIntensity = function(intensity)
{
	this._intensity = intensity < 1 ? 0 < intensity ? intensity : 0 : 1;
};
flux.Object2D.prototype.getIntensity = function()
{
	return this._intensity;
};
flux.Object2D.prototype.scale = function(v2, recursive)
{
	//if(recursive)
	//{
	//	var children = this._children;
	//	var L = children.length;
	//	for(var i = 0; i < L; i++)
	//	{
	//		children[i].scale(v2, recursive);
	//	}
	//}
};
flux.Object2D.prototype.getBounds = function(recursive)// TODO
{
	//var rect = arguments[1] || new flux.Rectangle(0, 0, 0, 0);
	//if(recursive)
	//{
	//	var cache;
	//	var i = (cache = this._children).length;
	//	while(i--)
	//	{
	//		cache[i].getBounds(recursive, rect, 1/*its not origin*/);
	//	}
	//	if(arguments[2]/*its origin?*/ && rect.isEmpty())
	//	{
	//		rect.x = rect.y = 0;
	//	}
	//}
	//return rect;
};
flux.Object2D.prototype.getRoot = function()
{
	var o = this._parent;
	while(o)
	o = this._parent;
	return o;
};
flux.Object2D.prototype.getParent = function()
{
	return this._parent;
};
flux.Object2D.prototype.getChild = function(index)
{
	return this._children[index];
};
flux.Object2D.prototype.getChildByName = function(name)
{
	if(name)
	{
		var children = this._children;
		var i = children.length;
		while(i--)
		{
			if(children[i].name === name)
				return i;
		}
	}
	return null;
};
flux.Object2D.prototype.getChildIndex = function(child)
{
	return this._children.indexOf(child);
};
flux.Object2D.prototype.getNumChildren = function()
{
	return this._children.length;
};
flux.Object2D.prototype.addChild = function(child)
{
	if( child instanceof flux.Object2D)
	{
		child.remove();
		(child._parent = this)._children[this._children.length] = child;
	}
};
flux.Object2D.prototype.addChildAt = function(child, index)
{

	//if( typeof index === 'number' && -1 < (index >>= 0) && child instanceof flux.Object2D)
	if(-1 < parseInt(index,10) && child instanceof flux.Object2D)
	{
		child.remove();
		//var children = this._children;
		//if (index < children.length)
		//{
        //    children.splice(index, 0, child);
        //}
        //else {
        //    children[children.length] = child;
        //}
        this._children.splice(index, 0, child);
		child._parent = this;
	}
};

flux.Object2D.prototype.remove = function()
{
	if (this._parent)
        this._parent.removeChild(this);
};

flux.Object2D.prototype.removeChild = function(child)
{
	var i = this._children.indexOf(child);
	if(i !== -1)
	{
		this._children.splice(i, 1);
		child._parent = null;
	}
};
flux.Object2D.prototype.removeChildAt = function(index)
{
	if(-1 < index) {
		if (index = this._children.splice(index, 1)[0]) index._parent = null;
	}
};
flux.Object2D.prototype.removeAll = function()
{
    var children = this._children;
    var i = children.length;
    while(i--) {
        children[i]._parent = null;
    }
	this._children = [];
};
flux.Object2D.prototype.setChildIndex = function(child, index)
{
	if(child && child._parent === this)
		this.addChildAt(child, index);

};
flux.Object2D.prototype.swapChildren = function(childA, childB)
{
	var children = this._children;
	if(( childA = children.indexOf(childA)) !== -1 && ( childB = children.indexOf(childB)) !== -1)
	{
		childA = children[childA];
		children[childA] = children[childB];
		children[childB] = childA;
	}
};
flux.Object2D.prototype.swapChildrenAt = function(indexA, indexB)
{
	var children = this._children;
	var length = children.length;
	if(-1 < indexA && indexA < length && -1 < indexB && indexB < length)
	{
		length = children[indexA];
		children[indexA] = children[indexB];
		children[indexB] = length;
	}
};
